/* Janus Chat - Version 1.0
 * Authors: Matthew Perry
 *          Michael Freid
 *          Marc Latou
 *          Richard Lee
 *
 * The Janus Chat Client is a highly secured
 * peer-to-peer chat client designed to facilitate
 * the transfer of sensitive data or as a communication
 * medium for the extra paranoid
 *
 * This file contains the header for the Chat class
 * which will encapsulate alot of the data for each
 * individual chat session */

#ifndef CHAT_H
#define CHAT_H

#include "message.h"
#include "user.h"
#include "jRsa.h"
#include "jAes.h"
#include <QTcpSocket>
#include <utility>
using std::pair;

class Chat : public QObject
{
    Q_OBJECT

    private:
        QTcpSocket* mySock;     /* The socket associated with this chat */
        bool connected;         /* A boolean to express the state of this chat */
        bool amiHost;           /* A boolean to express if this chat is host */
        User* me;               /* Pointer to information about the user */
        pair<string,jRsa::publicKey> peer;      /* Information about the peer */
        string mySessionKey;    /* AES Session Encryption Key */
        enum KeyState {         /* Enumeration of possible states for the key exchange */
            NoKeys = 0,
            HavePublic = 1,
            HaveSession = 2,
            Complete = 3
        };
        int myState;     /* The current state of the key exchange */

    public:
        Chat( User* myUser, bool host, QTcpSocket* sock );

        void startKeyExchange();                /* Begins the key exchange process */
        void writeMsg( Message& msg );    /* Given a message, it writes it to the socket */
        void disengage();                       /* This disconnects the socket from the peer */

    public slots:
        void readSome();                                        /* Activated by the socket's
                                                                 * readReady() signal, reads all messages */
    private slots:
        void socketError( QAbstractSocket::SocketError e );     /* Activated by the socket's
                                                                 * error() signal, displays error */
        void publicKeyStep( const string& pk );                 /* Slot for the public key step */
        void sessionKeyStep( const string& key );               /* Slot for the session key step */

    signals:
        void newMsg( const Message& msg, const QString& color ); /* Signal sent when a new message
                                                                  * has arrived, picked up by ChatTab */
        void disMsg( const QString& name );                      /* Signal sent when the peer has disconnected
                                                                  * picked up by the ChatTab */
        void publicKeyRecv( const string& pk );                  /* Signal sent out when the public key has
                                                                  * been recieved */
        void sessionKeyRecv( const string& key );                /* Signal sent out when the session key has
                                                                  * been recieved */
};

#endif // CHAT_H
